package com.tongren.framework.maintabfragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.tongren.framework.R;
import com.tongren.framework.core.BaseFragment;
import com.tongren.framework.core.ITabFragment;
import com.tongren.framework.sample.SampleListActivity;
import com.tongren.framework.sample.SampleListActivity1;
import com.tongren.framework.sample.SampleSectionListActivity;
import com.tongren.framework.school.QQPlayerActivity;
import com.tongren.framework.school.RTMPActivity;
import com.tongren.framework.school.VideoRecordActivity;
import com.tongren.framework.widgets.banner.BannerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MiddleFragment<T> extends BaseFragment implements ITabFragment,View.OnClickListener ,BannerView.OnBannerItemClickListener{
    // banner
    private BannerView mBannerView;
    // 菜单
    private GridView gv_menu;
    private List<Map<String, Object>> data_list;
    private SimpleAdapter sim_adapter;
//    //列表
//    protected BaseListAdapter adapter;
//    protected ArrayList<T> mDataList;
//    protected PullRecycler recycler;
    // 图片封装为一个数组
    private int[] icon = { R.drawable.ic_files, R.drawable.ic_book,
            R.drawable.ic_computer};
    private String[] iconName = { "我的课件", "我的作业", "班级创作"};
    @Override
    public void setUpView(View view) {
        mBannerView = (BannerView) view.findViewById(R.id.mBannerView);

        gv_menu = (GridView)view.findViewById(R.id.gv_menu);

//        recycler = (PullRecycler) view.findViewById(R.id.pullRecycler);
    }

    @Override
    public void setUpData() {
        // banners数据
        ArrayList<BannerView.Banner> banners = new ArrayList<>();
        banners.add(new BannerView.Banner(R.drawable.banner_anquan));
        banners.add(new BannerView.Banner(R.drawable.banner_fengcai));
        banners.add(new BannerView.Banner(R.drawable.banner_xiaoyuan));
        mBannerView.setUpData(banners, this);

        // grid
        //新建List
        data_list = new ArrayList<Map<String, Object>>();
        //获取数据
        getData();
        //新建适配器
        String [] from ={"image","text"};
        int [] to = {R.id.image,R.id.text};
        sim_adapter = new SimpleAdapter(this.getActivity(), data_list, R.layout.grid_item, from, to);
        //配置适配器
        gv_menu.setAdapter(sim_adapter);
        gv_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                switch (position){
                    case 0:
                        Intent intent = new Intent(getActivity(), SampleListActivity.class);
                        intent.putExtra("flag","t1");
                        startActivity(intent);
                        break;
                    case 1:
                        Intent intent2 = new Intent(getActivity(), SampleListActivity1.class);
                        intent2.putExtra("flag","t2");
                        startActivity(intent2);
                        break;
                    case 2:
                        Intent intent3 = new Intent(getActivity(), SampleSectionListActivity.class);
                        intent3.putExtra("flag","t3");
                        startActivity(intent3);
                        break;

                    default:

//                        Toast.makeText(getActivity(),"1",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        //
//        adapter = new ListAdapter();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_middle, container, false);
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onMenuItemClick() {

    }

    @Override
    public BaseFragment getFragment() {
        return this;
    }

    @Override
    public void onStart() {
        super.onStart();
        mBannerView.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
        mBannerView.onStop();
    }

    @Override
    public void onBannerClick(int index, BannerView.Banner banner) {
        Toast.makeText(getActivity(), "点击了"+index, Toast.LENGTH_SHORT).show();
    }

    public List<Map<String, Object>> getData(){
        //cion和iconName的长度是相同的，这里任选其一都可以
        for(int i=0;i<icon.length;i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", icon[i]);
            map.put("text", iconName[i]);
            data_list.add(map);
        }

        return data_list;
    }


}
